#include <QDebug>

#include <QCoreApplication>
#include <QStringList>
#include <QTextCodec>

#include "base2synsetconverter.h"
#include "ccl2baseconverter.h"
#include "documentcollection.h"

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    QTextCodec::setCodecForCStrings(QTextCodec::codecForName("UTF−8"));

    if(argc == 1){
        QTextStream in(stdin);
        QTextStream out(stdout);

        out << "loading index...";
        out.flush();
        DocumentCollection dc("/home/user/wwiizt/data/index");
        out << " done\n";
        out.flush();

        out << "\nEnter k\n";
        out.flush();
        int k;
        in >> k;
        in.flush();
        out << "\nEnter document name\n";
        out.flush();
        QString line;
        while((line = in.readLine()) != "exit"){
            if(!line.isEmpty()){
                if(dc.exists(line)){
                    const QMultiMap<double, QString> &res = dc.findSimilar(dc.document(line));
                    QMultiMap<double, QString>::const_iterator it = res.constEnd();
                    int counter = 0;
                    while(counter < k){
                        it--;
                        out << QString("%1 %2\n").arg(it.value(), 40).arg(it.key(), 10, 'g', 6);
                        counter++;
                    }
                    out.flush();
                }
                else{
                    out << QString("Document %1 does not exists\n").arg(line);
                    out.flush();
                }
                out << "\n";
                out.flush();
            }
        }
    }
    else if(argc == 2){
        if(a.arguments()[1] == "ccl2base"){
            Ccl2BaseConverter ccl2base;
            ccl2base.convertAll("/home/user/wwiizt/data/ccl", "/home/user/wwiizt/data/base");
        }
        else if(a.arguments()[1] == "base2synset"){
            Base2SynsetConverter base2synset;
            base2synset.convertAll("/home/user/wwiizt/data/base", "/home/user/wwiizt/data/synset");
        }
        else if(a.arguments()[1] == "index"){
            DocumentCollection dc;
            dc.index("/home/user/wwiizt/data/base", "/home/user/wwiizt/data/index");
        }
    }

    return 0;
}
